package com.okaixz.client.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.okaixz.client.mapper.KbCustomerAssistantMapper;
import com.okaixz.client.model.domain.KbAssistant;
import com.okaixz.client.model.domain.KbCustomerAssistant;
import com.okaixz.client.model.domain.User;
import com.okaixz.client.service.IKbAssistantService;
import com.okaixz.client.service.IKbCustomerAssistantService;
import com.okaixz.client.service.IUserService;
import com.okaixz.client.utils.SensitiveWordUtil;
import com.okaixz.common.client.exception.BusinessException;
import com.okaixz.common.client.exception.CommonErrorCode;
import com.okaixz.common.constant.CommonConstants;
import com.okaixz.manager.utils.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cdz
 * @since 2025-06-05
 */
@Service
public class KbCustomerAssistantServiceImpl extends ServiceImpl<KbCustomerAssistantMapper, KbCustomerAssistant> implements IKbCustomerAssistantService {

    @Autowired
    private IKbAssistantService kbAssistantService;
    @Autowired
    private IUserService userService;

    @Override
    public List<KbCustomerAssistant> getListbyUserId(String userId) {
        return lambdaQuery()
                .eq(KbCustomerAssistant::getUserId, userId)
                .list();
    }

    @Override
    public KbCustomerAssistant add(KbCustomerAssistant kbCustomerAssistant) {
        String assistantName = kbCustomerAssistant.getKbAssistantName();
        String userId = kbCustomerAssistant.getUserId();
        int httpVersion = BaseContext.getHttpVersion();
        if (httpVersion >= 1) {
            userId = BaseContext.getUserId();
            kbCustomerAssistant.setUserId(userId);
        }
        if (userId == null) {
            throw new BusinessException(CommonErrorCode.USER_NOT_EXIST);
        }
        User user = userService.getUserFormRedisOrDb(userId);
        if (user == null) {
            throw new BusinessException(CommonErrorCode.USER_NOT_EXIST);
        }

        if (assistantName == null) {
            throw new BusinessException(CommonErrorCode.CUSTOMER_ASSISTANT_NAME_EMPTY);
        }
        boolean contains = SensitiveWordUtil.contains(assistantName);
        if (contains) {
            throw new BusinessException(CommonErrorCode.SENSITIVE_WORDS);
        }

        String assistantIds = kbCustomerAssistant.getAssistantIds();
        if (assistantIds == null) {
            throw new BusinessException(CommonErrorCode.CUSTOMER_ASSISTANT_IDS_EMPTY);
        }
        String[] split = assistantIds.split(",");
        //查询标签列表
        Collection<KbAssistant> list = kbAssistantService.listByIds(Arrays.stream(split).collect(Collectors.toList()));
        if (list == null || list.size() == 0) {
            throw new BusinessException(CommonErrorCode.ASSISTANT_NOT_EXIST);
        }

        kbCustomerAssistant.setCreateTime(LocalDateTime.now());
//        kbCustomerAssistant.setAssistantUrl("/profile/upload/2025/customer_tab_"+user.getSex()+".png");
        kbCustomerAssistant.setSex(user.getSex());
        kbCustomerAssistant.setStatus(CommonConstants.STATUS_ENABLE);

        String url = "/profile/upload/2025/customer_tab_" + user.getSex() + ".png";
        url = new ArrayList<KbAssistant>(list).get(0).getIcon();
        kbCustomerAssistant.setAssistantUrl(url);
        //拼接输出风格和id
        String outStyles = list.stream()
                .map(i -> i.getOutStyle())
                .collect(Collectors.joining(","));
        String ids = list.stream()
                .map(i -> i.getId().toString())
                .collect(Collectors.joining(","));
        kbCustomerAssistant.setAssistantStyls(outStyles);
        kbCustomerAssistant.setAssistantIds(ids);

        boolean save = saveOrUpdate(kbCustomerAssistant);
        if (!save) {
            throw new BusinessException(CommonErrorCode.OP_FAIL);
        }
        return kbCustomerAssistant;
    }
}
